package br.com.classificadosonline.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.classificadosonline.Sistema;
import br.com.classificadosonline.nucleo.Pessoa;

/**
 * Servlet implementation class PublicarAnuncioServlet
 */
public class PublicarAnuncioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     private Sistema sistema = Sistema.getInstance();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PublicarAnuncioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int codCategoria = Integer.parseInt(request.getParameter("categorias"));
		int codPlano = Integer.parseInt(request.getParameter("planos"));
		String conteudo = request.getParameter("message");
		String UF = request.getParameter("uf");
		
		Pessoa pessoa = sistema.buscarPessoaPorUsuario(sistema.getUsuarioLogado().getUsuario());
		
		DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	    Date date = new Date();
	    String data = dateFormat.format(date);	    
	    //Calendar dataFim = Calendar.getInstance();
	    //Format formato = new SimpleDateFormat("MM/dd/yyyy - HH:mm:ss");
	    //dataFim.add(Calendar.DATE, sistema.buscarPlano(codPlano).getDias());
	    //System.out.println(dataFim.toString());
		sistema.cadastrarAnuncio(data, data, data, conteudo, 123456, 1, codPlano, codCategoria, pessoa.getCpf());
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("publicaranunciofinalizado.jsp");
		dispatcher.forward(request, response);
		return;
	}

}
